/**
 * 权限
 * WangPeixi
 */
var table = "";
var code="";
var t=1;
var parentCode = '';
$(function() {
	parentCode = request.QueryString("parentCode");// 点击左侧菜单传过来的按钮父节点
	code = postAsyncAjaxText("/findCodeListAndMap", null).data;
	code = eval("("+code+")");
	table = $("#limitsList");
	initGrid();//初始化表格
	initReload();
});

function initGrid(){
	//查询权限按钮
	var data = postAsyncAjax("/selectLimitsByUserIdAndParentCode", {parentCode:parentCode});
	if (data.data == null)
		data.data = [];
	var arr = data.data;
	var param = [];
	var addrArray = [];
	for (var i = 0; i < arr.length; i++) {
		addrArray[arr[i].name] = arr[i].addr;
		param.push({
			text : arr[i].name,
			iconCls : arr[i].icon,
			handler : function() {
				eval(addrArray[$(this).text()]);
			}
		})
	}
	
	table.treegrid( {	
		iconCls : 'icon-ok',
		idField:'id',    
	    treeField:'name',
		striped : true,
		collapsible : false,
		fitColumns :true,
		singleSelect : true,
		animate:true,
		columns : [ [  {
			field : 'id',
			checkbox : true,
			hidden:true
		}, {
			title : '菜单名称',
			field : 'name',
			width : 180
		}, {
			title : '图标',
			field : 'icon',
			width : 80
		}, {
			title : 'URL',
			field : 'url',
			width : 80
		}, {
			title : '排序',
			field : 'sort',
			width : 80
		}
		] ],
		toolbar : param,
		onBeforeLoad:function(){
			
			var param = [];
			var limit = postAsyncAjax("/findLimitsByParentCode?parentCode=-1").data;
			
			for(var i = 0; i<limit.length; i++){
				param.push({
					"id" : limit[i].id,"name" : limit[i].name,"icon" : limit[i].icon,"url" : limit[i].addr,
					"state" : "closed","sort" : limit[i].sort,"t":t,"code":limit[i].code,iconCls: limit[i].icon
					})
			}
			console.log(param);
			table.treegrid('loadData', param);
			table.treegrid("expandAll");
		},
		onBeforeExpand : function(note) {
			 if(note.t==1){
				 userTreeLoad(note);
			 }
		 }

	});
	
}
	
treeReload =function(){
	var taba = parent.$("#menutab").tabs('getSelected'); // 获取选择的面板
	var title = taba.panel("options").tab.text();
	parent.$("#" + title).get(0).contentWindow.$("#limitsList").treegrid('load');
}


function userTreeLoad(note) {
	var param = new Array();
	var limit = postAsyncAjax("/findLimitsByParentCode?parentCode="+note.code).data;
	for(var i = 0; i<limit.length; i++){
		param.push({
			"id" : limit[i].id,"name" : limit[i].name,"icon" : limit[i].icon,"url" : limit[i].addr,
			"state" : "closed","sort" : limit[i].sort,"t":t,"code":limit[i].code,iconCls: limit[i].icon
		})
	}
	$('#limitsList').treegrid('append', {
		parent : note.id,
		data : param
	});
	note.t =2;
}


addBean = function(){
	var url = "/view/page/limits/addLimits.html?id=1";
	topWindow(url, "新增",{width:700,height:500});
}


updateBean = function(){
	var rows = table.datagrid('getSelected');
	if(null == rows){
		$.messager.alert("消息提示","至少选择一条记录","warning");
		return;
	}
	
	if(1 == rows.id){
		$.messager.alert("消息提示","首页不允许修改","warning");
		return;
	}
	
	
	var url = "/view/page/limits/updateLimits.html?id="+rows.id;
	topWindow(url, "修改",{width:700,height:500});
}

delBean = function(url){

	var rows = table.datagrid('getSelected');
	if(null == rows){
		$.messager.alert("消息提示","至少选择一条记录","warning");
		return;
	}
	var id = rows.id;
	$.messager.confirm('确认对话框', '确认删除？', function(r){
		if (r){
		   var data = postAsyncAjax(url, {"id":id});
		   if(data.returnCode == "00"){
			   top.showMessage("删除成功!");
			   gridReload("limitsList");
		   }
		}
	});
}



initReload = function(){
	$("#btn").click(function(){
		table.datagrid("reload");
	});
}
